package com.feige.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feige.constant.Constants;
import com.feige.pojo.Exercise;
import com.feige.result.AjaxResult;
import com.feige.service.ExerciseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 习题集 前端控制器
 * </p>
 *
 * @author feige
 * @since 2021-04-01
 */
@Api(tags = "习题集前端控制器")
@RestController
@RequestMapping("/exercise/exercise")
public class ExerciseController {
    @Resource
    private ExerciseService exerciseService;

    @PostMapping("/save")
    public AjaxResult save(@RequestBody Exercise exercise){
        boolean save = exerciseService.save(exercise);
        if (save){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @ApiOperation(value = "学生获取习题")
    @GetMapping("/{pageNum}/{exerciseTitleId}")
    public AjaxResult get(
            @PathVariable Integer pageNum,
            @PathVariable String exerciseTitleId
    ){
        Page<Exercise> page = new Page<>(pageNum, Constants.PAGE_SIZE * 4);
        QueryWrapper<Exercise> wrapper = new QueryWrapper<>();
        wrapper
                .select("id","type_value","title","option_list","point")
                .eq("exercise_title_id",exerciseTitleId)
                .orderByAsc("type_value");
        exerciseService.page(page,wrapper);
        AjaxResult success = AjaxResult.success(page.getRecords());
        success.put("total",page.getTotal());
        return success;
    }

    @ApiOperation(value = "学生获取有答案和解析的习题")
    @GetMapping("/answer/{pageNum}/{exerciseTitleId}/{userId}")
    public AjaxResult getExerciseVo(
            @PathVariable Integer pageNum,
            @PathVariable String exerciseTitleId,
            @PathVariable String userId){
        return exerciseService.getExerciseVo(pageNum,exerciseTitleId,userId);
    }

    @ApiOperation(value = "教师获取习题")
    @GetMapping("/tea/{pageNum}/{exerciseTitleId}")
    public AjaxResult getTea(
            @PathVariable Integer pageNum,
            @PathVariable String exerciseTitleId
    ){
        Page<Exercise> page = new Page<>(pageNum, Constants.PAGE_SIZE * 4);
        QueryWrapper<Exercise> wrapper = new QueryWrapper<>();
        wrapper.
                select("id","type_value","title","option_list","answer","parse","point")
                .eq("exercise_title_id",exerciseTitleId)
                .orderByAsc("type_value");
        exerciseService.page(page,wrapper);
        AjaxResult success = AjaxResult.success(page.getRecords());
        success.put("total",page.getTotal());
        return success;
    }

    @ApiOperation(value = "修改习题")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody Exercise exercise){
        boolean update = exerciseService.updateById(exercise);
        if (update){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @ApiOperation(value = "删除习题")
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable String id){
        boolean delete = exerciseService.removeById(id);
        if (delete){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }
}

